/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
export var search = function (nums, target) {
  let left = 0
  let right = nums.length - 1
  while (left <= right) {
    let mid = Math.floor((left + right) / 2)
    if (nums[mid] === target) {
      return mid
    }
    if (nums[left] <= nums[mid]) {
      // 说明左边是有序的
      if (nums[left] <= target && target < nums[mid]) {
        // 判断target是否在左边是的话取左右
        right = mid - 1
      } else {
        left = mid + 1
      }
    } else {
      // 说明右边是有序的，判断target是否在右边
      if (nums[mid] < target && target <= nums[right]) {
        left = mid + 1
      } else {
        right = mid - 1
      }
    }
  }
  return -1
}
